package com.polarrose.wsf.stats;

import java.util.concurrent.atomic.AtomicLong;

public class CallStatistics {

	private String callName;
	private final AtomicLong totalInvocations = new AtomicLong(0L);
	private final AtomicLong totalFailures = new AtomicLong(0L);
	private final AtomicLong totalResponseTime = new AtomicLong(0L);

	public CallStatistics(String callName) {
		this.callName = callName;
	}

	public synchronized void incrementCounters(boolean wasSuccess, long responseTime) {
		totalInvocations.incrementAndGet();
		if (!wasSuccess) {
			totalFailures.incrementAndGet();
		}
		totalResponseTime.addAndGet(responseTime);

		if (totalResponseTime.get() < 0 || totalInvocations.get() < 0) {
			// resetting stats after 0x7fffffffffffffffL requests ;)
			totalInvocations.set(0L);
			totalFailures.set(0L);
			totalResponseTime.set(0L);
		}
	}

	public void setCallName(String callName) {
		this.callName = callName;
	}

	public String getCallName() {
		return callName;
	}

	public long getTotalFailures() {
		return totalFailures.get();
	}

	public long getAverageResponseTime() {
		return totalResponseTime.get() / totalInvocations.get();
	}

	public long getTotalInvocations() {
		return totalInvocations.get();
	}
}
